package com.suns.sharephoto.service.impl;

import com.suns.sharephoto.common.PageResult;
import com.suns.sharephoto.entity.User;
import com.suns.sharephoto.mapper.PhotoMapper;
import com.suns.sharephoto.mapper.UserMapper;
import com.suns.sharephoto.service.UserService;
import com.suns.sharephoto.vo.PhotoVO;
import com.suns.sharephoto.vo.UserStatsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    
    @Autowired
    private PhotoMapper photoMapper;
    
    @Autowired
    private UserMapper userMapper;
    
    private static final int PAGE_SIZE = 10;

    @Override
    public UserStatsVO getUserStats(Integer userId) {
        // 获取用户信息
        User user = userMapper.findById(userId);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }

        // 构建统计数据
        UserStatsVO stats = new UserStatsVO();
        stats.setOpenid(user.getOpenid());
        stats.setTotalPhotos(photoMapper.countUserPhotos(userId));
        stats.setTotalLikes(photoMapper.countUserTotalLikes(userId));
        
        return stats;
    }

    @Override
    public PageResult getUserPhotos(Integer userId, Integer page) {
        // 计算偏移量
        int offset = (page - 1) * PAGE_SIZE;
        
        // 获取照片列表
        List<PhotoVO> photos = photoMapper.getUserPhotos(userId, offset, PAGE_SIZE);
        Integer total = photoMapper.countUserPhotos(userId);
        
        return new PageResult(photos, total);
    }

    @Override
    public void deletePhoto(Integer photoId, Integer userId) {
        // 删除照片，同时验证是否是该用户的照片
        Integer result = photoMapper.deletePhoto(photoId, userId);
        if (result == 0) {
            throw new RuntimeException("删除失败，照片不存在或无权限删除");
        }
    }
} 